﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AzureTosser.Services.POCImplementations
{
    public class POCQueuedPipe: IPipe
    {
        public POCQueuedPipe(string id)
        {
            Id = id;
        }
        public string Id { get; private set; }
        public event EventHandler<MessageEventArgs> MessageAdded;
        private Queue<Message> mQueue = new Queue<Message>();
        public void Push(Message message)
        {
            lock (mQueue)
            {
                mQueue.Enqueue(message);
            }
            if (MessageAdded != null)
                MessageAdded(this, new MessageEventArgs(message));
        }

        public Message Peek()
        {
            lock (mQueue)
            {
                if (mQueue.Count > 0)
                    return mQueue.Peek();
                else
                    return null;
            }
        }

        public Message Pop()
        {
            lock (mQueue)
            {
                if (mQueue.Count > 0)
                    return mQueue.Dequeue();
                else
                    return null;
            }
        }
    }
}